package cn.cytong.util.ext

/**
 * Created by orangeJ on 2017/5/28.
 */

private val HEX_CHARS = "0123456789ABCDEF".toCharArray()

fun ByteArray.toHex(): String {
    return toHex(size)
}

fun ByteArray.toHex(len: Int): String {
    val result = StringBuffer()

    repeat(Math.min(len, size), {
        val octet = this[it].toInt()
        val firstIndex = (octet and 0xF0).ushr(4)
        val secondIndex = octet and 0x0F
        result.append(HEX_CHARS[firstIndex])
        result.append(HEX_CHARS[secondIndex])
    })

    return result.toString()
}